from Selenium2Library.keywords.keywordgroup import KeywordGroup
from Selenium2Library.locators import ElementFinder

class _BreezePayeeListKeywords(KeywordGroup):
    """Library for Breeze Payee List Keywords purposes.
    
    This library is used in Breeze projects.
    """
    def __init__(self):
        """Does nothing. init only"""
        print '_BreezePayeeListKeywords.__init__'
    
    def read_bmw_payee(self):
        """Used in payee-list.tsv it is used for payee info comparing between BMW and iBanking. 
        Including the payee for Bill payment and IBFT(others).
        
        This keyword is used for reading payee in BMW. First read bill payment payee then read IBFT.
        
        See `Read Ibnk Payee`
        """
        return self._run('read_bmw_payee')
 
    def _read_bmw_payee_pk(self):
        #read bill payee
        payees = []
        
        if self._is_text_present("My Bills"):
            element = self._element_find("//*[@id='BP']/a", True, False)
            element.click()
            self.wait_until_page_contains_element("//dl[@class='heading collapsable']/dd[@style]/ul/li")

            rows3 = self._element_find("//dt[@id='BP']/following-sibling::dd/ul/li", False, False)
            self._info(len(rows3))
            
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./a/span[3]")
                benefiname = rows3[i].find_elements_by_xpath("./a/span[1]")
                bankname = rows3[i].find_elements_by_xpath("./a/span[2]")
#                currency = rows3[i].find_elements_by_xpath("./a/span[3]")
                
                self._info('%s> %s> %s>' % (account[0].text.split(" ")[0].replace(",", ""), benefiname[0].text, bankname[0].text))
                payees.append([account[0].text.split(" ")[0].replace(",", ""),
                                benefiname[0].text,
                                bankname[0].text])
            self._info(len(payees))
        else:
            pass
        
        #read IBFT payee
        if self._is_text_present("Other Accounts"):
            element = self._element_find("//*[@id='IBFT']/a", True, False)
            element.click()
            self.wait_until_page_contains_element("//dt[@id='IBFT']/following-sibling::dd/ul/li")

            rows3 = self._element_find("//dt[@id='IBFT']/following-sibling::dd/ul/li", False, False)
            self._info(len(rows3))
            
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./a/span[3]")
                benefiname = rows3[i].find_elements_by_xpath("./a/span[1]")
                bankname = rows3[i].find_elements_by_xpath("./a/span[2]")
#                currency = rows3[i].find_elements_by_xpath("./a/span[3]")
                
                self._info('%s> %s> %s>' % (account[0].text.split(" ")[0].replace(",", ""), benefiname[0].text, bankname[0].text))
                payees.append([account[0].text,
                                benefiname[0].text,
                                bankname[0].text])
            self._info(len(payees))
        else:
            pass
        
        payees.sort()
        return payees
   
    def _read_bmw_payee_id(self):
        '''For BMW ID, please refer to BMW PK if not work here
        '''
        if self._is_text_present("Other Accounts"):
            element = self._element_find("//*[@id='IBFT']/a", True, False)
            element.click()
            self.wait_until_page_contains_element("//dl[@class='heading collapsable']/dd[@style]/ul/li")

            payees = []
#            rows3 = self._element_find("//dt[@id='IBFT']/following-sibling::dd/ul/li[@id='7168138183635170786']", False, False)
            rows3 = self._element_find("//dt[@id='IBFT']/following-sibling::dd/ul/li", False, False)
            self._info(len(rows3))
#            self._info(self.get_element_attr(rows3[0], "id"))
            
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./a/span[3]")
                benefiname = rows3[i].find_elements_by_xpath("./a/span[1]")
                bankname = rows3[i].find_elements_by_xpath("./a/span[2]")
                currency = rows3[i].find_elements_by_xpath("./a/span[3]")
                
                self._info('%s> %s> %s> %s>' % (account[0].text.split(" ")[0].replace(",", ""), benefiname[0].text, bankname[0].text, currency[0].text.split(" ")[1]))
                payees.append([account[0].text.split(" ")[0].replace(",", ""),
                                benefiname[0].text,
                                bankname[0].text,
                                currency[0].text.split(" ")[1]])
            self._info(len(payees))
            return payees
        else:
            return payees
        
    def read_ibnk_payee(self):
        """Used in payee-list.tsv it is used for payee info comparing between BMW and iBanking. 
        Including the payee for Bill payment and IBFT(others).
        
        This keyword is used for reading payee in iBanking. First read bill payment payee then read IBFT.
        
        See `Read BMW Payee`
        """
        return self._run('read_ibnk_payee')
    
    def _read_ibnk_payee_pk(self):
        #read bill payee
        element = self._element_find("//*[@id='parentMenuHead'][5]/a/table/tbody/tr/td", True, False)
        self._info(element.text)
        element.click()
        self.wait_until_page_contains("Pay a Registered Bill Payee ")
        
        rows3 = self._element_find("//table[@id='registerBillers']/tbody/tr", False, False)
        self._info(len(rows3))
        
        payees = []
        
        if self._is_text_present("No Beneficiaries Available"):
            self._info("No Beneficiaries Available IFT")
        else:
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./td[2]")
                benefiname = rows3[i].find_elements_by_xpath("./td[1]")
                bankname = rows3[i].find_elements_by_xpath("./td[1]")
#                currency = rows3[i].find_elements_by_xpath("./td[2]")
                self._info('%s> %s> %s>' % (account[0].text, benefiname[0].text.split('-')[1], bankname[0].text.split('-')[0]))
                
                if len(bankname[0].text.split('-')) != 2:
                    self._info(bankname[0].text.split('-')[2])
                    payees.append([account[0].text,
                                    benefiname[0].text.split('-')[1] + "-" + benefiname[0].text.split('-')[2],
                                    bankname[0].text.split('-')[0]])                    
                else:
                    payees.append([account[0].text,
                                benefiname[0].text.split('-')[1],
                                bankname[0].text.split('-')[0]])
            self._info(len(payees))
            
        #read IBFT payee
        element = self._element_find("//*[@id='parentMenuHead'][7]/a/table/tbody/tr/td", True, False)
        element.click()
        self.wait_until_page_contains("Funds Transfer Between Own Accounts")
        
        element = self._element_find("//*[@id='parentMenuContentSelected']/div[2]/a/table/tbody/tr/td", True, False)
        self._info(element.text)
        element.click()
        self.wait_until_page_contains("Add Beneficiary")

        rows3 = self._element_find("//*[@id='vPayeeList']/tbody/tr", False, False)
        self._info(len(rows3))
        
        if self._is_text_present("No Beneficiaries Available"):
            self._info("No Beneficiaries Available IFT")
        else:
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./td[2]")#PKR, 18960077001, STANDARD CHARTERED BANK
                benefiname = rows3[i].find_elements_by_xpath("./td[1]")
              
                payees.append([(account[0].text.split(',')[1].strip()+" "+account[0].text.split(',')[0]),
                            benefiname[0].text,
                            account[0].text.split(',')[2].strip()])
            self._info(len(payees))

        payees.sort()
        return payees
        
    def _read_ibnk_payee_id(self):
        self.select_links_frame_ibanking()
        self.current_frame_contains("Accounts")
        
        #click to show beneficiary page
        element = self._element_find("//*[@id='nav_ML281']", True, False)
        element.click()
        self.wait_until_page_contains("Maintain   Beneficiary")
        self.select_top_frame_ibanking()
        self.wait_until_page_contains("Fund Transfers")
        self.select_links_frame_ibanking()
        element = self._element_find("//*[@id='Maintain <br> Beneficiary']", True, False)
        element.click()
        self.select_top_frame_ibanking()
        self.wait_until_page_contains("Add & Maintain Beneficiary")
        
        #click to show Account to Account Transfer Beneficiary
        element = self._element_find("//*[@id='pagePrintTag']/table/tbody/tr[3]/td/a", True, False)
        element.click()
        self.wait_until_page_contains("Account to Account Beneficiary")

        payees = []
        
        #Account to Account Beneficiary
        rows3 = self._element_find("//*[@id='pagePrintTag']/table/tbody/tr[@class='table_row_white']", False, False)
        self._info(len(rows3))
        
        if self._is_text_present("No Beneficiaries Available"):
            self._info("No Beneficiaries Available for OAT")
        else:
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./td[1]")
                benefiname = rows3[i].find_elements_by_xpath("./td[3]")
                bankname = rows3[i].find_elements_by_xpath("./td[4]")
                currency = rows3[i].find_elements_by_xpath("./td[5]")
                self._info('%s> %s> %s> %s>' % (account[0].text, benefiname[0].text, bankname[0].text, currency[0].text))
                payees.append([account[0].text,
                                benefiname[0].text,
                                bankname[0].text,
                                currency[0].text])
            self._info(len(payees))

        self.select_links_frame_ibanking()
        element = self._element_find("//*[@id='Maintain <br> Beneficiary']", True, False)
        element.click()
        self.select_top_frame_ibanking()
        self.wait_until_page_contains("Add & Maintain Beneficiary")
        
        #click to show Local Payment Transfer Beneficiary
        element = self._element_find("//*[@id='pagePrintTag']/table/tbody/tr[5]/td/a", True, False)
        element.click()
        self.wait_until_page_contains("Local Payment Transfer Beneficiaries")

        #Local Payment Transfer Beneficiary
        rows3 = self._element_find("//*[@id='pagePrintTag']/table/tbody/tr[@class='table_row_white']", False, False)
        self._info(len(rows3))
        
        if self._is_text_present("No Beneficiaries Available"):
            self._info("No Beneficiaries Available for LPT")
        else:
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./td[1]")
                benefiname = rows3[i].find_elements_by_xpath("./td[3]")
                bankname = rows3[i].find_elements_by_xpath("./td[4]")
                currency = rows3[i].find_elements_by_xpath("./td[5]")
                self._info('%s> %s> %s> %s>' % (account[0].text, benefiname[0].text, bankname[0].text, currency[0].text))
                
                payees.append([account[0].text,
                                benefiname[0].text,
                                bankname[0].text,
                                currency[0].text])
            self._info(len(payees))
        
        self.select_links_frame_ibanking()
        element = self._element_find("//*[@id='Maintain <br> Beneficiary']", True, False)
        element.click()
        self.select_top_frame_ibanking()
        self.wait_until_page_contains("Add & Maintain Beneficiary")
        
        #click to show Outward Telegraphic Transfer Beneficiary
        element = self._element_find("//*[@id='pagePrintTag']/table/tbody/tr[7]/td/a", True, False)
        element.click()
        self.wait_until_page_contains("Outward Telegraphic Transfer Beneficiary")
        
        #Outward Telegraphic Transfer Beneficiary
        rows3 = self._element_find("//*[@id='pagePrintTag']/table/tbody/tr[@class='table_row_white']", False, False)
        self._info(len(rows3))
        
        if self._is_text_present("No Beneficiaries Available"):
            self._info("No Beneficiaries Available IFT")
        else:
            for i in range(0, len(rows3)): #TODO
                account = rows3[i].find_elements_by_xpath("./td[1]")
                benefiname = rows3[i].find_elements_by_xpath("./td[2]")
                bankname = rows3[i].find_elements_by_xpath("./td[2]")
                currency = rows3[i].find_elements_by_xpath("./td[3]")
                self._info('%s> %s> %s> %s>' % (account[0].text, benefiname[0].text, bankname[0].text, currency[0].text))
                
                payees.append([account[0].text,
                                benefiname[0].text,
                                bankname[0].text,
                                currency[0].text])
            self._info(len(payees))
        return payees



